#include "countdown.h"
#include "rotary_encoder.h"
#include "segment_codes.h"
#include "ht1621b.h"
#include "LOG.h"
#include <stdio.h>
#include <string.h>

// 倒计时事件回调函数指针
static countdown_event_callback_t event_callback = NULL;

// 倒计时显示状态（用于闪烁冒号）
static bool colon_visible = true;

// 初始化倒计时模块
void countdown_init(void)
{
    // 初始化倒计时时间为0
    memset(&down_time, 0, sizeof(tick_time_t));
    
    // 初始化倒计时状态
    down_time.status = TICK_STATUS_STOPPED;
    
    colon_visible = true;
    LOG_INFO("Countdown module initialized");
}

// 设置倒计时时间
void countdown_set_time(uint8_t minutes, uint8_t seconds)
{
    // 限制输入范围
    if (minutes > 59) minutes = 59;
    if (seconds > 59) seconds = 59;

    down_time.countdown_time.minutes = minutes;
    down_time.countdown_time.seconds = seconds;
    down_time.countdown_time.milliseconds = 0;
    
    LOG_INFO("Countdown time set to %d:%d", minutes, seconds);
}

// 获取倒计时时间
void countdown_get_time(uint8_t *minutes, uint8_t *seconds)
{  
    if (minutes) *minutes = down_time.countdown_time.minutes;
    if (seconds) *seconds = down_time.countdown_time.seconds;
}

// 启动倒计时
void countdown_start(void)
{  
    if (down_time.status != TICK_STATUS_RUNNING) {
        down_time.status = TICK_STATUS_RUNNING;
        LOG_INFO("Countdown started");
    }
}

// 停止倒计时
void countdown_stop(void)
{
    if (down_time.status != TICK_STATUS_STOPPED) {
        down_time.status = TICK_STATUS_STOPPED;
        LOG_INFO("Countdown stopped");
    }
}

// 设置倒计时
void countdown_setting(void)
{
    if (down_time.status != TICK_STATUS_SETTING)
    {
        down_time.status = TICK_STATUS_SETTING;
        LOG_INFO("Countdown setting mode activated");
    }
}

// 暂停倒计时
void countdown_pause(void)
{
    if (down_time.status == TICK_STATUS_RUNNING) {
        down_time.status = TICK_STATUS_PAUSED;
        LOG_INFO("Countdown paused");
    } 
}

// 重置倒计时
void countdown_reset(void)
{
    down_time.status = TICK_STATUS_STOPPED;
    // 重置时间为0
    memset(&down_time, 0, sizeof(tick_time_t));
    LOG_INFO("Countdown reset");
}

void countdown_param_update(void)
{
    // 更新倒计时时间
    if (down_time.countdown_time.minutes == 0 && down_time.countdown_time.seconds == 0 && down_time.countdown_time.milliseconds == 0)
    {
        // 倒计时结束
        down_time.status = TICK_STATUS_PAUSED;

        // 调用回调函数通知倒计时完成
        if (event_callback != NULL)
        {
            event_callback();
        }
    }
    else
    {
        if (down_time.countdown_time.milliseconds > 0)
        {
            down_time.countdown_time.milliseconds--;
        }
        else
        {
            if (down_time.countdown_time.seconds > 0)
            {
                down_time.countdown_time.milliseconds = 99;
                down_time.countdown_time.seconds--;
            }
            else
            {
                if (down_time.countdown_time.minutes > 0)
                {
                    down_time.countdown_time.seconds = 59;
                    down_time.countdown_time.minutes--;
                }
            }
        }
    }
}

// 更新倒计时（每秒调用一次）
void countdown_update(bool colon_visible)
{
    if ((down_time.status == TICK_STATUS_RUNNING) || (down_time.status == TICK_STATUS_PAUSED)) {
        
        // 显示倒计时时间（格式：分:秒）
        segment_display_RTC_time(down_time.countdown_time.minutes, down_time.countdown_time.seconds, down_time.countdown_time.milliseconds, colon_visible);
        segment_display_function_indicator(4, true);

        countdown_param_update();

    }
}

// 获取倒计时状态
tick_status_t countdown_get_status(void)
{
    return down_time.status;
}

// 处理旋转编码器事件
void countdown_handle_rotary_event(void)
{
    rotary_event_t rotary_event = rotary_encoder_process();
    
    switch (rotary_event) {
        case ROTARY_EVENT_CLOCKWISE:
            // 顺时针旋转增加时间
            if (down_time.status == TICK_STATUS_SETTING)
            {
                // 增加秒数
                if (down_time.countdown_time.seconds < 59) {
                    down_time.countdown_time.seconds++;
                } else {
                    down_time.countdown_time.seconds = 0;
                    if (down_time.countdown_time.minutes < 59) {
                        down_time.countdown_time.minutes++;
                    }
                }
                // 重置毫秒为0
                down_time.countdown_time.milliseconds = 0;
                
                LOG_INFO("Countdown time increased to %d:%d", 
                         down_time.countdown_time.minutes, down_time.countdown_time.seconds);
                
                // 更新显示
                ht1621b_clear_buffer();
                segment_display_RTC_time(0, down_time.countdown_time.minutes, down_time.countdown_time.seconds, true);
                ht1621b_display_update();
            }
            break;
            
        case ROTARY_EVENT_COUNTERCLOCKWISE:
            // 逆时针旋转减少时间
            if (down_time.status == TICK_STATUS_SETTING)
            {
                // 减少秒数
                if (down_time.countdown_time.seconds > 0) {
                    down_time.countdown_time.seconds--;
                } else {
                    down_time.countdown_time.seconds = 59;
                    if (down_time.countdown_time.minutes > 0) {
                        down_time.countdown_time.minutes--;
                    }
                }
                // 重置毫秒为0
                down_time.countdown_time.milliseconds = 0;
                
                LOG_INFO("Countdown time decreased to %d:%d", 
                         down_time.countdown_time.minutes, down_time.countdown_time.seconds);
                
                // 更新显示
                ht1621b_clear_buffer();
                segment_display_RTC_time(0, down_time.countdown_time.minutes, down_time.countdown_time.seconds, true);
                ht1621b_display_update();
            }
            break;
            
        case ROTARY_EVENT_BUTTON_PRESSED:
            // 按钮按下确认设置
            if (down_time.status == TICK_STATUS_SETTING) {
                down_time.status = TICK_STATUS_STOPPED;
                LOG_INFO("Countdown setting confirmed");
            }
            break;
            
        case ROTARY_EVENT_BUTTON_LONG_PRESSED:
            // 长按重置倒计时
            countdown_reset();
            LOG_INFO("Countdown reset by long press");
            
            // 更新显示
            ht1621b_clear_buffer();
            segment_display_RTC_time(0, down_time.countdown_time.minutes, down_time.countdown_time.seconds, true);
            ht1621b_display_update();
            break;
            
        case ROTARY_EVENT_NONE:
        default:
            // 无事件
            break;
    }
}

// 检查倒计时是否完成
bool countdown_is_finished(void)
{
    return (down_time.status == TICK_STATUS_FINISHED);
}

// 注册倒计时事件回调函数
void countdown_register_callback(countdown_event_callback_t callback)
{
    event_callback = callback;
}
